package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo07 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();

        //得到数据库连接
        try(Connection conn = DBUtils.getConn();){
//            //创建执行SQL语句的对象
//            Statement s = conn.createStatement();
//            ResultSet rs = s.executeQuery(
//                    "select count(*) from user where username='"
//                            +username+"' and password='"+password+"'");
            //让游标往下移动  指向返回的数据
            //通过PreparedStatement对象解决SQL注入的问题
            String sql = "select count(*) from where username=? and password=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            //把?替换成用户名和密码
            ps.setString(1,username);
            ps.setString(2,password);
            //执行SQL语句
            ResultSet rs = ps.executeQuery();
            rs.next();
            //取出查询到的数据  1代表查询到的数据位置
            int count = rs.getInt(1);
            if (count>0){
                System.out.println("登录成功!");
            }else {
                System.out.println("用户名或密码输入不正确,请重新输入");
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
    }
}
